<?php

Class TitleStudiesController extends AppController {

    public $name = 'TitleStudies';

    public function add($property_id) {
        $this->set('property_id', $property_id);
        if (empty($this->data)) {
            $this->set('predio', $this->TitleStudy->Property->find('first', array('recursive' => -1, 'conditions' => array('Property.id' => $property_id), 'joins' => array(array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => 'City.id=Property.city_id'), array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => 'City.departament_id=Departament.id')), 'fields' => array('Property.*', 'Departament.nombre', 'City.nombre'))));
        } else {

            if ($this->TitleStudy->save($this->data)) {
                $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'TitleStudies', 'action' => 'index', $property_id));
            } else {
                $this->Session->setFlash('Error Guardando datos');
            }
        }
    }

    public function edit($id) {
        $this->TitleStudy->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->TitleStudy->find('first', array('conditions' => array('TitleStudy.id' => $id), 'fields' => array('TitleStudy.id', 'TitleStudy.nombre_predio', 'TitleStudy.titulo', 'TitleStudy.codigo_catastral', 'TitleStudy.direccion', 'TitleStudy.area_titulo', 'TitleStudy.area_predio', 'TitleStudy.area_permuta', 'TitleStudy.cabida_linderos', 'TitleStudy.hipoteca', 'TitleStudy.valor_hipoteca', 'TitleStudy.medida_cautelar', 'TitleStudy.medida_cautelar_observacion', 'TitleStudy.falsa_tradicion', 'TitleStudy.falsa_tradicion_observacion', 'TitleStudy.servidumbre', 'TitleStudy.servidumbre_observacion', 'TitleStudy.desenglobes', 'TitleStudy.desenglobes_observacion', 'TitleStudy.valoracion', 'TitleStudy.valoracion_observacion', 'TitleStudy.lesion_enorme', 'TitleStudy.lesion_enorme_observacion', 'TitleStudy.divisiones_materiales', 'TitleStudy.divisiones_materiales_observacion', 'TitleStudy.baldio', 'TitleStudy.baldio_observacion', 'TitleStudy.condicion_resolutoria', 'TitleStudy.resolutoria_observacion', 'TitleStudy.afectaciones', 'TitleStudy.afectaciones_observacion', 'TitleStudy.patrimonio_familiar', 'TitleStudy.patrimonio_familiar_observacion', 'TitleStudy.extincion_dominio', 'TitleStudy.extincion_observacion', 'TitleStudy.pleno_dominio', 'TitleStudy.pleno_observacion', 'TitleStudy.otro', 'TitleStudy.otro_observacion', 'TitleStudy.documentos_analizados', 'TitleStudy.concepto', 'TitleStudy.calificacion', 'TitleStudy.property_id', 'TitleStudy.id')));
            $this->set('predio', $this->TitleStudy->Property->find('first', array('recursive' => -1, 'conditions' => array('Property.id' => $this->data['TitleStudy']['property_id']), 'joins' => array(array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => 'City.id=Property.city_id'), array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => 'City.departament_id=Departament.id')), 'fields' => array('Property.*', 'Departament.nombre', 'City.nombre'))));
        } else {

            if ($this->TitleStudy->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'TitleStudies', 'action' => 'index', $this->data['TitleStudy']['property_id']));
            } else {
                $this->Session->setFlash('Error editando datos');
            }
        }
    }

    public function index($property_id) {
        $this->set('property_id', $property_id);
        $this->TitleStudy->recursive = 0;
        $this->paginate = array('TitleStudy' => array('maxLimit' => 500, 'limit' => 50, 'fields' => array('TitleStudy.id', 'TitleStudy.concepto', 'TitleStudy.calificacion', 'TitleStudy.id', 'Property.nombre')));
        $this->set('TitleStudies', $this->paginate(array('TitleStudy.property_id' => $property_id)));
    }

    public function delete($id, $property_id) {
        if ($this->TitleStudy->delete($id)) {
            $this->Session->setFlash('Registro borrado correctamente', 'flash_custom');
            $this->redirect(array('controller' => 'TitleStudies', 'action' => 'index', $property_id));
        }
    }

}

?>